/*
 * Copyright 2013 University of Padua, Italy
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package it.dei.unipd.healthservice.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import it.dei.unipd.healthservice.resource.*;

/**
 * Crea una nuova fabbrica
 * 
 * @author Giorgio Vitale
 * @version 1.00
 */
public class CreateFabbricaDatabase {
	/**
	 * Query di inserimento nuova fabbrica nel database
	 */
	private static final String STATEMENT = "INSERT INTO fabbrica("+
            "login, password, stato, provincia, indirizzo, cap, citta)"+
            "VALUES (?, ?, ?, ?, ?, ?, ?)";
	
	/**
	 * Query per estrazione password della fabbrica
	 */
	private static final String STATEMENT_LOGIN = "SELECT password FROM fabbrica WHERE login = ?;";
	
	/**
	 * Connessione al database
	 */
	private final Connection con;
	/**
	 * Fabbrica da creare
	 */
	private final Fabbrica fabbrica;
	/**
	 * Crea una nuova fabbrica
	 * 
	 * @param con connessione al database.
	 * @param fabbrica fabbrica da creare.
	 */
	public CreateFabbricaDatabase(Connection con, Fabbrica fabbrica) {
		this.con = con;
		this.fabbrica = fabbrica;
	}
	/**
	 * crea la fabbrica.
	 * 
	 * @throws SQLException
	 *             in caso di problemi nell'accesso al database.
	 */
	public void createFabbrica() throws SQLException {

		PreparedStatement pstmt = null;
		ResultSet rs = null;

		try {
			pstmt = con.prepareStatement(STATEMENT);
			pstmt.setString(1, fabbrica.getLogin());
			pstmt.setString(2, fabbrica.getPassword());
			pstmt.setString(3, fabbrica.getStato());
			pstmt.setString(4, fabbrica.getProvincia());
			pstmt.setString(5, fabbrica.getIndirizzo());
			pstmt.setString(6, fabbrica.getCap());
			pstmt.setString(7, fabbrica.getCitta());
			if (fabbrica.getLogin()==null || fabbrica.getLogin().length()==0)
				throw new SQLException("Login non valida");
			if (fabbrica.getCap()==null || fabbrica.getCap().length()!=5)
				throw new SQLException("Cap non valido");
			
			pstmt.execute();
		}
		finally {
			if (rs != null)
				rs.close();
			if (pstmt != null)
				pstmt.close();
			con.close();
		}
	}
	
	public boolean existsFabbrica() throws SQLException {

		PreparedStatement pstmt = null;
		ResultSet rs = null;

		try {
			pstmt = con.prepareStatement(STATEMENT_LOGIN);
			pstmt.setString(1, fabbrica.getLogin());
			pstmt.execute();
			rs = pstmt.getResultSet();
			if(rs.next())
			{ String pwd = rs.getString("password");
			  if(pwd.equals(fabbrica.getPassword()))
					return true;
			}
			return false;
		} catch(Exception e){return false;}
		finally {
			if (rs != null)
				rs.close();
			if (pstmt != null)
				pstmt.close();
			con.close();
		}
	}

}